<?php

namespace Tbf\Bundle\PackageBundle;

use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Routing\RouteCollection;
use Tbf\Bundle\PackageBundle\ClassGenerator\EntityGenerator;

class PackageManager
{
    /** @var PackageInterface[]  */
    protected $package_list = array();
    /** @var ControllerConfigInterface[] */
    protected $controller_list = array();
    protected $eg;
    public function __construct(EntityGenerator $eg)
    {
        $this->eg = $eg;
    }
    public function addPackage(PackageInterface $package)
    {
        $this->package_list[] = $package;
    }
    public function addControllerConfig(ControllerConfigInterface $controller)
    {
        $this->controller_list[] = $controller;
    }
    /**
     * @return PackageInterface[]
     */
    public function getPackageList()
    {
        return $this->package_list;
    }
    public function dumpEntity(OutputInterface $output=null)
    {
        if ($output===null) {
            $output = new NullOutput();
        }
        foreach ($this->package_list as $package) {
            $output->writeln('dump package '.get_class($package));
            foreach ($package->getEntitiesDefine() as $entity_define) {
                $this->eg->generateToFile($entity_define);
            }
        }
    }

    /**
     * @return RouteCollection
     */
    public function getRouteCollection()
    {
        $rc = new RouteCollection();
        foreach ($this->package_list as $package) {
            $rc->addCollection($package->getRouteCollection());
        }
        foreach ($this->controller_list as $controller) {
            $rc->addCollection($controller->getRouteCollection());
        }

        return $rc;
    }
}
